#include "clock.h"


Clock::Clock()
{
    timecal = millis()-30000;

    scr = lv_obj_create(NULL, NULL);
    lv_obj_set_style_local_bg_color(scr, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
	lv_scr_load(scr);

    lv_style_init(&font_style_time);
	lv_style_set_text_font(&font_style_time, LV_STATE_DEFAULT, &lv_font_montserrat_48);
	lv_style_set_text_color(&font_style_time, LV_STATE_DEFAULT, LV_COLOR_WHITE);

    lv_style_init(&font_style_date);
	lv_style_set_text_font(&font_style_date, LV_STATE_DEFAULT, &lv_font_montserrat_34);
	lv_style_set_text_color(&font_style_date, LV_STATE_DEFAULT, LV_COLOR_WHITE);

    label_date = lv_label_create(scr, NULL);
	lv_obj_add_style(label_date, LV_LABEL_PART_MAIN, &font_style_date);
    lv_label_set_text(label_date, "loading...");
    lv_obj_align(label_date, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_task_handler();

    lv_obj_align(label_date, NULL, LV_ALIGN_CENTER, 0, 0);
    
    label_time = lv_label_create(scr, NULL);
	lv_obj_add_style(label_time, LV_LABEL_PART_MAIN, &font_style_time);

    img_time_hr0 = lv_img_create(scr, NULL);
    img_time_hr1 = lv_img_create(scr, NULL);
    img_time_mn0 = lv_img_create(scr, NULL);
    img_time_mn1 = lv_img_create(scr, NULL);
    lv_img_set_src(img_time_hr0, &clock_icon_list[0]);
    lv_img_set_src(img_time_hr1, &clock_icon_list[0]);
    lv_img_set_src(img_time_mn0, &clock_icon_list[0]);
    lv_img_set_src(img_time_mn1, &clock_icon_list[0]);
    lv_obj_align(img_time_hr0, NULL, LV_ALIGN_CENTER, -90, -35);
    lv_obj_align(img_time_hr1, NULL, LV_ALIGN_CENTER, -35, -35);
    lv_obj_align(img_time_mn0, NULL, LV_ALIGN_CENTER, 35, -35);
    lv_obj_align(img_time_mn1, NULL, LV_ALIGN_CENTER, 90, -35);
    lv_label_set_text(label_time, ":");
    lv_obj_align(label_time, NULL, LV_ALIGN_CENTER, 0, -40);
    lv_obj_align(label_date, NULL, LV_ALIGN_CENTER, -15, 35);
    // tft.setAddrWindow(0L, 140L, 100L, 100L);
    // display_buf = new uint8_t[RGB565_BUFFER_SIZE];
}

Event Clock::main_process(Action act)
{
    event = NOEVENT;
    if (is_update==2)
        show_time();
    else
    {
        is_update = (WiFi.status()==WL_CONNECTED)&&update_time();
        if (is_update==0)
        {
            timer.setTime(0, 1, 0, 1, 1, 2022);
            event = RED;    
        } 
        else if(is_update==1)
            event = GREEN;
        is_update=2;   
    }
    return event;
}

void Clock::show_time()
{
    if (millis()-timecal>30000&&!file_pilot.available())
    {
        time = timer.getTime();//.substring(0, 5);
        date = timer.getDate();
        lv_img_set_src(img_time_hr0, &clock_icon_list[time.substring(0, 1).toInt()]);
        lv_img_set_src(img_time_hr1, &clock_icon_list[time.substring(1, 2).toInt()]);
        lv_img_set_src(img_time_mn0, &clock_icon_list[time.substring(3, 4).toInt()]);
        lv_img_set_src(img_time_mn1, &clock_icon_list[time.substring(4, 5).toInt()]);
        date = date.substring(date.indexOf(",")+1, date.length()) + "\n             " +date.substring(0, date.indexOf(","));
        lv_label_set_text(label_date, date.c_str());
        lv_task_handler();
        timecal = millis();
    }
    // play_ani();
}
void Clock::play_ani()
{
    
    tft.startWrite();
    if (file_pilot.available())
    {
    for (int i=0;i<4;i++){
        l = file_pilot.read(display_buf, RGB565_BUFFER_SIZE);
        tft.pushColors(display_buf, l);
        delay(100);
        }
    }
    else
    {
        file_pilot=SD.open("/clock/pilot.rgb");
        tft.setAddrWindow(0L, 140L, 100L, 100L);
    }
    tft.endWrite();
}

bool Clock::update_time()
{
	String Time = "";
	HTTPClient http;
    bool is_update;
	http.begin("http://quan.suning.com/getSysTime.do");
    // http.begin("http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp");
	// start connection and send HTTP headerFFF
	int httpCode = http.GET();
	// httpCode will be negative on error
	if (httpCode > 0)
	{
        // Serial.println("aaa");
		// file found at server
		if (httpCode == HTTP_CODE_OK)
		{
            Time = http.getString();
            // Serial.println(Time);
            int pos = Time.indexOf("sysTime1");
            Time = Time.substring(pos + 11, pos + 25);
            int yr = Time.substring(0, 4).toInt();
            int mt = Time.substring(4, 6).toInt();
            int dy = Time.substring(6, 8).toInt();
            int hr = Time.substring(8, 10).toInt();
            int mn = Time.substring(10, 12).toInt();
            int sc = Time.substring(12, 14).toInt();
            timer.setTime(sc, mn, hr, dy, mt, yr);
            is_update = true;
		}
        else{
            is_update = false;
        }
	}
	else
	{
		// Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        is_update = false;
	}
    http.end();
    return is_update;
}

Clock:: ~Clock()
{
    lv_obj_del(scr);
    // delete display_buf;
}